using System.Collections.Generic;

namespace NhibernateDemoCore
{
    public class Customer
    {
        private ICollection<Order> orders = new HashSet<Order>();

        public virtual long Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual Address BillingAddress { get; set; }

        public virtual IEnumerable<Order> Orders
        {
            get { return orders; }
        }

        public virtual bool Equals(Customer other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.Id == 0 || Id == 0
                       ? Equals(other.Name, Name)
                       : other.Id == Id;
        }

        public virtual Order CreateOrder(string product)
        {
            var order = new Order(this);
            order.Add(product);
            
            orders.Add(order);
            
            return order;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj.GetType() == typeof (Customer) && Equals((Customer) obj);
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }

        public static bool operator ==(Customer left, Customer right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(Customer left, Customer right)
        {
            return !Equals(left, right);
        }
    }
}